<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "article".
 *
 * @property string $id
 * @property integer $user_id
 * @property string $title
 * @property string $content
 * @property integer $create_time
 * @property integer $views
 * @property integer $update_time
 * @property integer $release_time
 * @property integer $article_status
 * @property integer $sys_category_id
 * @property integer $is_deleted
 * @property string $label_img
 */
class Article extends \yii\db\ActiveRecord
{
    const ARTICLE_STATUS_RELEASE = '0';
    const ARTICLE_NOT_DELETED = '0';
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'article';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'title', 'create_time', 'views', 'update_time', 'release_time', 'article_status', 'sys_category_id', 'is_deleted'], 'required'],
            [['user_id', 'create_time', 'views', 'update_time', 'release_time', 'article_status', 'sys_category_id', 'is_deleted'], 'integer'],
            [['content'], 'string'],
            [['title'], 'string', 'max' => 50],
            [['label_img'], 'string', 'max' => 80],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'title' => 'Title',
            'content' => 'Content',
            'create_time' => 'Create Time',
            'views' => 'Views',
            'update_time' => 'Update Time',
            'release_time' => 'Release Time',
            'article_status' => 'Article Status',
            'sys_category_id' => 'Sys Category ID',
            'is_deleted' => 'Is Deleted',
            'label_img' => 'Label Img',
        ];
    }

    public function getTags()
    {
        return $this->hasMany(Tag::className(),['id'=>'tag_id'])->viaTable('tag_and_article',['model_id'=>'id']);
    }

    public function getCatefory()
    {
        return $this->hasOne(SysCategory::className(),['id'=>'sys_category_id']);
    }

    public function getTag()
    {
        $tagNames = [];
        foreach ($this->tags as $tag){
            $tagNames[] = $tag->name;
        }
        return implode(',',$tagNames);
    }

}
